﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
//using ;
using MSHTML;

namespace myie.code.pharaoh.com
{
    public class Script
    {
        //public void Click()
        //{
        //    Mouse.Click();
        //}
        private string _scriptPath;
        public FormIE form;
        public WebBrowser web;
//        public int X
//        {
//        	get {return web.PointToScreen(new System.Drawing.Point(0,0)).X; }
//        }
//        public int Y
//        {
//            get {return web.PointToScreen(new System.Drawing.Point(0,0)).Y; }
//        }
        public Script(FormIE aform,WebBrowser aWeb)
        {
            _scriptPath = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "script");
            this.form = aform;
            web = aWeb;
        }

		public void ClickInCurPostion()
		{
			form.Focus();
			Mouse.Click();
		}
        public void Click(int x,int y)
        {
        	form.Focus();
            Mouse.Click(x,y);
        }
        public void Move(int x,int y)
        {
        	form.Focus();
            Mouse.SetPos(x,y);
        }   
        public void MoveTo(int dx,int dy)
        {
        	form.Focus();
            Mouse.Move(dx,dy);
        }
        
        public void SendKeys(string keys)
        {
        	//form.SendKeys(keys);
        	System.Windows.Forms.SendKeys.Send(keys);
        }
        public void Sleep(int ms)
        {
        	System.Threading.Thread.Sleep(ms);
        }
        
        public void Go(string url)
        {
        	web.Navigate(url);
        }
        
        public void AddButton(string Name,string scriptName)
        {
        	form.AddButton(Name,scriptName);
        	//form
        }
        
        public void ClearUserButton()
        {
        	form.ClearUserButton();
        }
        
        public void SetFormSize(int width,int height)
        {
        	form.Width=width;
        	form.Height=height;
        	if(form.WindowState!= FormWindowState.Normal)
        	{
        		form.WindowState=FormWindowState.Normal;
        	}
        }
        
        public void RunScript(string script)
        {
        	try
        	{
        	IHTMLWindow2 win = (IHTMLWindow2)web.Document.Window.DomWindow;
        	win.execScript(script,"javascript");
        	}
        	catch(Exception exp)
        	{
        		MessageBox.Show("执行脚本错误!\n"+exp.ToString());
        	}
        }
        
        public void LoadScript(string scriptFn)
        {
            //string fn=scriptFn.Replace('\\','/');
            string fn = scriptFn;
            if(!System.IO.File.Exists(fn))
            {
                fn = System.IO.Path.Combine(_scriptPath, scriptFn);
            }
            if (System.IO.File.Exists(fn))
            {
            	//MSHTML.IHTMLWindow2 win = (MSHTML.IHTMLWindow2)web.Document.Window.DomWindow;

                string js = System.IO.File.ReadAllText(fn);
                RunScript(js);
            }
            else
            {
               MessageBox.Show("脚本不存在!\n"+scriptFn);
            }
            
        }
    }
}
